package cn.iocoder.yudao.module.call.controller.admin.taskcalleelist;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.call.controller.admin.taskcalleelist.vo.CallTaskCalleeListAppendReqVO;
import cn.iocoder.yudao.module.call.controller.admin.taskcalleelist.vo.CallTaskCalleeListPageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.taskcalleelist.vo.CallTaskCalleeListRespVO;
import cn.iocoder.yudao.module.call.controller.admin.taskcalleelist.vo.CallTaskCalleeListSaveReqVO;
import cn.iocoder.yudao.module.call.dal.dataobject.taskcalleelist.CallTaskCalleeListDO;
import cn.iocoder.yudao.module.call.mq.producer.taskcalleelistimport.TaskCalleeListImportProducer;
import cn.iocoder.yudao.module.call.service.taskcalleelist.CallTaskCalleeListService;
import cn.iocoder.yudao.module.call.service.taskcalleelistnumberextend.CallTaskCalleeListNumberExtendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 群呼任务呼叫名单")
@RestController
@RequestMapping("/call/task-callee-list")
@Validated
@Slf4j
public class CallTaskCalleeListController {

    @Resource
    private CallTaskCalleeListService taskCalleeListService;


    @Resource
    private TaskCalleeListImportProducer taskCalleeListImportProducer;

    @PostMapping("/create")
    @Operation(summary = "创建群呼任务呼叫名单")
    @OperateLog(enable = false)
    public CommonResult<Long> createTaskCalleeList(
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam("name") String name,
            @RequestParam("verify") Integer verify,
            @RequestParam("unique") Integer unique
    ) {
        @Valid CallTaskCalleeListSaveReqVO createReqVO = new CallTaskCalleeListSaveReqVO();
        createReqVO.setOwnerUserId(getLoginUserId());
        createReqVO.setName(name);
        createReqVO.setVerify(verify);
        createReqVO.setUnique(unique);
        Long id = taskCalleeListService.createTaskCalleeList(createReqVO);
        //导入呼叫名单
        try {
            if (ObjectUtil.isNotEmpty(file)) {
                File file1 = FileUtil.createTempFile("call-callee-list-files", file.getOriginalFilename(), false);
                FileUtil.writeBytes(file.getBytes(), file1.getPath());
                taskCalleeListImportProducer.sendTaskCalleeListImportMessage(file1.getPath(), id, null, createReqVO.getVerify(), unique);
            }
        } catch (Exception e) {
            log.info("exception:{}", e.getMessage());
            throw exception(new ErrorCode(100500, e.getMessage()));
        }
        return success(id);
    }

    @PostMapping("/append")
    @Operation(summary = "追加呼叫名单")
    @OperateLog(enable = false)
    public CommonResult<Boolean> appendTaskCalleeList(@Valid CallTaskCalleeListAppendReqVO reqVO) {
        MultipartFile file = reqVO.getFile();
        //导入呼叫名单
        try {
            if (ObjectUtil.isNotEmpty(file)) {
                File file1 = FileUtil.createTempFile("call-callee-list-files", file.getOriginalFilename(), false);
                FileUtil.writeBytes(file.getBytes(), file1.getPath());
                taskCalleeListImportProducer.sendTaskCalleeListImportMessage(file1.getPath(), reqVO.getId(), null, reqVO.getVerify(), reqVO.getUnique());
            }
        } catch (Exception e) {
            log.info("exception:{}", e.getMessage());
            throw exception(new ErrorCode(100500, e.getMessage()));
        }
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新群呼任务呼叫名单")
    public CommonResult<Boolean> updateTaskCalleeList(@Valid @RequestBody CallTaskCalleeListSaveReqVO updateReqVO) {
        taskCalleeListService.updateTaskCalleeList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除群呼任务呼叫名单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTaskCalleeList(@RequestParam("id") Long id) {
        taskCalleeListService.deleteTaskCalleeList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得群呼任务呼叫名单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CallTaskCalleeListRespVO> getTaskCalleeList(@RequestParam("id") Long id) {
        CallTaskCalleeListDO taskCalleeList = taskCalleeListService.getTaskCalleeList(id);
        return success(BeanUtils.toBean(taskCalleeList, CallTaskCalleeListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得群呼任务呼叫名单分页")
    public CommonResult<PageResult<CallTaskCalleeListRespVO>> getTaskCalleeListPage(@Valid CallTaskCalleeListPageReqVO pageReqVO) {
        PageResult<CallTaskCalleeListDO> pageResult = taskCalleeListService.getTaskCalleeListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CallTaskCalleeListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出群呼任务呼叫名单 Excel")
    @OperateLog(type = EXPORT)
    public void exportTaskCalleeListExcel(@Valid CallTaskCalleeListPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CallTaskCalleeListDO> list = taskCalleeListService.getTaskCalleeListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "群呼任务呼叫名单.xls", "数据", CallTaskCalleeListRespVO.class,
                BeanUtils.toBean(list, CallTaskCalleeListRespVO.class));
    }


}